package com.shop.common.tmpl;

import com.alibaba.fastjson.JSON;
import com.shop.common.model.InputBean;
import com.shop.common.model.ResponseBen;
import com.shop.common.model.ServiceBean;
import com.shop.common.util.state.ResultCode;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    public static<T> String returnJSON(Integer code,String msg,T data ){
        ResponseBen<T> responseBen =new ResponseBen<T>(code,msg,data);
        return JSON.toJSONString(responseBen);
    }

    public static String returnSvc(ServiceBean serviceBean){
        ResponseBen responseBen =new ResponseBen();
        if (serviceBean.isSuccess()){
            responseBen.setCode(ResultCode.SUCCESS.value());
            responseBen.setMsg(serviceBean.getMsg());
            responseBen.setData(serviceBean.getData());
            responseBen.setTotal(serviceBean.getMaxSize());
            responseBen.setTtl(serviceBean.getTtl());
        }else {
            responseBen.setCode(ResultCode.FAILED.value());
            responseBen.setMsg(serviceBean.getMsg());
            responseBen.setTtl(serviceBean.getTtl());
        }
        return JSON.toJSONString(responseBen);
    }

    public static String returnSuccess(){
        ResponseBen responseBen =new ResponseBen(ResultCode.SUCCESS.value(),null,null);
        return JSON.toJSONString(responseBen);
    }

    public static String returnSuccess(String msg){
        ResponseBen responseBen =new ResponseBen(ResultCode.SUCCESS.value(),msg,null);
        return JSON.toJSONString(responseBen);
    }

    public static String returnFailed(){
        ResponseBen responseBen =new ResponseBen(ResultCode.FAILED.value(),null,null);
        return JSON.toJSONString(responseBen);
    }

    public static String returnFailed(String msg){
        ResponseBen responseBen =new ResponseBen(ResultCode.FAILED.value(),msg,null);
        return JSON.toJSONString(responseBen);
    }


    public static String returnException(){
        ResponseBen responseBen =new ResponseBen(ResultCode.EXCEPTION.value(),null,null);
        return JSON.toJSONString(responseBen);
    }

    public static String returnCode(Integer code){
        ResponseBen responseBen =new ResponseBen(code,null,null);
        return JSON.toJSONString(responseBen);
    }

    public static String returnMsg(Integer code,String msg){
        ResponseBen responseBen =new ResponseBen(code,msg,null);
        return JSON.toJSONString(responseBen);
    }

    public static<T> String returnData(T data){
        ResponseBen<T> responseBen =new ResponseBen<T>(ResultCode.SUCCESS.value(),"",data);
        String result = JSON.toJSONString(responseBen);
        return result;
    }
    public static<T> InputBean newParamMode(){
        return new InputBean<T>();
    }

    public static<T> InputBean newParamMode(T data){
        return new InputBean<T>(data);
    }

    public static<T> InputBean newParamMode4P(T data, Integer index, Integer pageSize){
        return new InputBean<T>(data,index,pageSize);
    }

    public static String getToken(HttpServletRequest request){
        return request.getHeader("token");
    }
}
